//
//  GYWelcomeViewController.m
//  GoodLuck
//
//  Created by apple on 2018/4/20.
//  Copyright © 2018年 yunjiazhineng. All rights reserved.
//

static NSString *welcomeCellIdentifier = @"welcomeCellIdentifier";

#import "GYWelcomeViewController.h"
#import "GYNetWorkTools.h"
#import "GYWelcomeCollectionViewCell.h"

@interface GYWelcomeViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (weak, nonatomic) IBOutlet UICollectionView *welcomeCollectionView;

/** 数据源 */
@property (nonatomic, strong) NSArray *images;
@end

@implementation GYWelcomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //初始化
    [self setupUI];
}

- (void)setupUI {
    //注册cell
    [self.welcomeCollectionView registerNib:[UINib nibWithNibName:@"GYWelcomeCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:welcomeCellIdentifier];
    self.welcomeCollectionView.pagingEnabled = YES;
    self.welcomeCollectionView.bounces = NO;
    self.welcomeCollectionView.showsHorizontalScrollIndicator = NO;
    
    //加载数据
    [GYNetWorkTools requestWelcomeInfo:^(NSDictionary *obj) {
        //NSLog(@"imageOBJ=======%@",obj);
        int code = [[obj objectForKey:@"code"] intValue];
        if (code == 200) {
            dispatch_group_t group = dispatch_group_create();
            NSArray *datas = [obj objectForKey:@"datas"];
            if ([datas isKindOfClass:[NSArray class]]) {
                if (datas.count == 0) {
                    [[NSNotificationCenter defaultCenter]postNotificationName:@"changedViewController" object:nil];
                }else {
                    self.images = datas;
                    [self.welcomeCollectionView reloadData];
//                    for (NSDictionary *dic in datas) {
//                        dispatch_group_enter(group);
//                        NSString *url = [dic objectForKey:@"url"];
//                        [[SDWebImageManager sharedManager] loadImageWithURL:[NSURL URLWithString:url] options:0 progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
//                            if (finished) {
//                                [self.images addObject:image];
//                            }
//                            dispatch_group_leave(group);
//                        }];
//                    }
//
//                    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
//                        [self.welcomeCollectionView reloadData];
//                    });
                }
            }else {
                [[NSNotificationCenter defaultCenter]postNotificationName:@"changedViewController" object:nil];
            }
        }else {
            [[NSNotificationCenter defaultCenter]postNotificationName:@"changedViewController" object:nil];
        }
    } error:^(NSError *error) {
        NSLog(@"error=======%@",error);
        //如果网络请求失败则直接更换
        [[NSNotificationCenter defaultCenter]postNotificationName:@"changedViewController" object:nil];
    }];
}

#pragma mark -- UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return self.images.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 1;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    GYWelcomeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:welcomeCellIdentifier forIndexPath:indexPath];
    NSDictionary *imageDic = self.images[indexPath.section];
    [cell.welcomeImageView sd_setImageWithURL:[NSURL URLWithString:[imageDic objectForKey:@"url"]]];
    cell.show = indexPath.section == (self.images.count-1) ? NO : YES;
    
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(ScreenWidth, ScreenHeight);
}


#pragma mark -- 懒加载

@end
